package com.bjshiwei.zhengu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageCache;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bjshiwei.zhengu.requestInfo.BitmapCache;
import com.bjshiwei.zhengu.requestInfo.ImageRequestErrorInfo;
import com.bjshiwei.zhengu.requestInfo.ImageRequestInfo;
import com.bjshiwei.zhengu.requestInfo.RequestError;
import com.bjshiwei.zhengu.requestInfo.RequestErrorInfo;
import com.bjshiwei.zhengu.requestInfo.RequestListenerInfo;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

//import com.google.gson.Gson;

/**
  * @desc 请求处理基类
  * @author sanjin
  * @time 2016/9/23 0023 下午 1:11
  */
public class VolleyRequest {
    private Context context;
    private RequestQueue imageRequestQueue;
    private RequestQueue jsonRequestQueue;
    private ImageLoader imageLoader;
    private ImageCache imageCache;
    private static VolleyRequest instance = null;
    private static JSONObject jsonObject;
    private static Gson mGson = new Gson();

    private VolleyRequest(Context context) {
        this.context = context;
        this.jsonRequestQueue = Volley.newRequestQueue(context);
        this.imageRequestQueue = Volley.newRequestQueue(context);
//        this.imageCache = BitmapCache.getInstance();
        this.imageCache  = new BitmapCache();
        this.imageLoader = new ImageLoader(imageRequestQueue, imageCache);
    }

    public static synchronized VolleyRequest getInstance(Context context) {
        if (null == instance) {
            instance = new VolleyRequest(context);
        }
        return instance;
    }

    public ImageLoader getImageLoader() {return imageLoader;
    }

    public RequestQueue getJsonRequestQueue() {
        return jsonRequestQueue;
    }

    public static Gson getGsonInstance() {
        return mGson;
    }

    //----------------------------------------------------------------------------------------------
    public void jsonPoastRequest(String url, final Map<String, String> params, RequestListenerInfo requestListener) {
        final String mRequestBody = appendParameter(url, params);
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, requestListener, new RequestError()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                for (int a = 0; a < params.size(); a++) {

                }
                return params;
            }

            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
            }

            @Override
            public byte[] getBody() {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            mRequestBody, PROTOCOL_CHARSET);
                    return null;
                }
            }
        };
        jsonRequestQueue.add(request);
    }

    private String appendParameter(String url, Map<String, String> params) {
        Uri uri = Uri.parse(url);
        Uri.Builder builder = uri.buildUpon();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.appendQueryParameter(entry.getKey(), entry.getValue());
        }
        return builder.build().getQuery();
    }

    public void jsonGetRequest(String url, JSONObject jsonRequset, RequestListenerInfo requestListener, RequestErrorInfo requestErrorInfo) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, jsonRequset, requestListener,requestErrorInfo);
        jsonRequestQueue.add(request);
    }



    public void stringPoastRequest(String url, final Map<String, String> map, RequestListenerInfo requestListener) {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, requestListener, new RequestError()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        jsonRequestQueue.add(stringRequest);
    }

    public void stringGetRequest(String url, final Map<String, String> map, RequestListenerInfo requestListener) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, requestListener, new RequestError()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        jsonRequestQueue.add(stringRequest);
    }

    public void imageRequest(String url, ImageRequestInfo requestInfo, ImageRequestErrorInfo errorInfo) {
        ImageRequest imageRequest = new ImageRequest(url, requestInfo, 720, 300, Bitmap.Config.ARGB_8888, errorInfo);
        imageRequestQueue.add(imageRequest);
    }

    public void getImage(String url, ImageView imageView, int ress, int resss) {
        ImageLoader.ImageListener l = ImageLoader.getImageListener(imageView, ress, resss);
        imageLoader.get(url, l);
    }

    public void getImage(String url, ImageView imageView, int ress, int resss, int with, int hint) {
        ImageLoader.ImageListener l = ImageLoader.getImageListener(imageView, ress, resss);
        imageLoader.get(url, l, with, hint);
    }

    public JSONObject getJsonInstance() {
        if (null == jsonObject) {
            jsonObject = new JSONObject();
        }
        return jsonObject;
    }



}
